//
//  ICReminderButton.m
//  ICome
//
//  Created by zhangrongwu on 2017/7/29.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICReminderButton.h"
#import "NSString+Extension.h"
#import "ICHeaderFile.h"

@interface ICReminderButton()

@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, strong) UIView * contentV;

@end

@implementation ICReminderButton


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.contentV];
        [self nameLabel];
        [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
            make.width.mas_equalTo(App_Frame_Width-60);
            make.height.mas_equalTo(11);
        }];

    }
    return self;
}

- (void)setMessageModel:(ICMessageModel *)messageModel
{
    _messageModel = messageModel;
    ICMessage *message = messageModel.message;
    NSDictionary *dic = [NSDictionary dictionaryWithJsonString:message.fileKey];
    
    self.nameLabel.text = message.content;
    
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(20);
        make.top.equalTo(self.mas_top).offset(12);
        make.right.equalTo(self.mas_right).offset(-20);
    }];
    
    [self layoutIfNeeded];
    [self.contentV removeAllSubviews];
    NSArray *contentList = dic[@"questions"];
    CGFloat contentHeight = 0;
    for(NSInteger i = 0;i < contentList.count;i++){
        NSString *title = [contentList objectAtIndex:i];
        CGSize size = [title sizeWithMaxWidth:253 - 50 andFont:[UIFont systemFontOfSize:14]];
        [self addView:title  count:i  size:size topheight:contentHeight];
        
        contentHeight += (24 + 15);
       
    }
    
    [self.contentV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight + 12);
    }];
    
    // 强制更新
    [self layoutIfNeeded];
}

-(void)addView:(NSString *)title  count:(NSUInteger)count  size:(CGSize)size topheight:(CGFloat)topheight
{
    UIButton *btn = [[UIButton alloc] init];
    [self.contentV addSubview:btn];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 12;
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = ICRGB(0x87c2ff).CGColor;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.contentEdgeInsets   = UIEdgeInsetsMake(1, 5, 1, 5);
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:ICRGB(0x3498fe) forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:ICRGB(0xd2e8ff)] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:ICRGB(0x87c2ff)] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(messageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
   
    [btn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentV.mas_left).offset(20);
        make.top.equalTo(self.contentV.mas_top).offset(topheight);
        NSLog(@"%f", topheight);
        make.width.mas_equalTo(size.width + 20);
        make.height.mas_equalTo(24);
    }];
}

- (void)messageBtnClick:(UIButton *)sender {
    if (self.reminderAction) {
        self.reminderAction(sender.titleLabel.text);
    }
}



#pragma mark - Getter

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = ICRGB(0x333333);
        label.numberOfLines = 0;
        [self addSubview:label];
        _nameLabel = label;
    }
    return _nameLabel;
}

-(UIView *)contentV
{
    if(!_contentV)
    {
        _contentV = [[UIView alloc] init];
        _contentV.backgroundColor = [UIColor clearColor];
    }
    return _contentV;
}

@end
